<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API 请求示例</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"></script>
    <style>
        /* 自定义样式 */
        body {
            font-family: Arial, sans-serif;
        }
        #responseBox {
            height: 120px;
            resize: none;
        }
    </style>
</head>
<body class="bg-light">

<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-lg-6 col-md-8">
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="card-title">请求后端接口</h5>
                </div>
                <div class="card-body">
                    <!-- 参数输入框 -->
                    <div class="mb-3">
                        <label for="paramInput" class="form-label">请输入完整参数，例如MUSIC_U=xxxx</label>
                        <textarea type="text" class="form-control" id="paramInput" name="paramInput">
                            MUSIC_U=xxxx
                        </textarea>
                    </div>

                    <!-- 提交按钮 -->
                    <button id="submitButton" class="btn btn-primary w-100">提交请求</button>
                </div>
            </div>

            <!-- 显示返回信息的文本框 -->
            <div class="card shadow-sm mt-4">
                <div class="card-header bg-secondary text-white">
                    <h5 class="card-title">实时响应内容</h5>
                </div>
                <div class="card-body">
                    <textarea id="responseBox" class="form-control h-100" rows="5" readonly></textarea>
                </div>
            </div>
            <div class="card shadow-sm mt-4">
                <div class="card-header bg-secondary text-white">
                    <h5 class="card-title">操作说明</h5>
                </div>
                <div class="card-body">
                    <p class="card-text">
                        1. 扫码登录网易云https://music.163.com/<br>
                        2.操作步骤如下所示
                        <img src="1.png" alt="图片示例" style="max-width: 100%;">
                    </p>
                </div>
            </div>
        </div>

        </div>
    </div>
</div>


<script>    $(document).ready(function () {
    // 创建WebSocket实例
    var socket = new WebSocket('ws://119.3.239.188:56085/sailing/websocket');

    // 连接成功时的回调函数
    socket.onopen = function(event) {
        console.log("WebSocket已连接");
    };

    // 接收到服务器消息时的回调函数
    socket.onmessage = function(event) {
        var existingText = $('#responseBox').val();
        var newText = event.data + '\n'; // 在新消息后添加换行符
        $('#responseBox').val(existingText + newText);
        // 在追加内容后立即滚动到底部
        scrollToBottom();
    };
    // 定义滚动到底部的方法
    function scrollToBottom() {
        var textarea = document.getElementById('responseBox');
        textarea.scrollTop = textarea.scrollHeight;
    }
    // 发送按钮点击事件
    $('#submitButton').click(function () {
        // 获取输入框中的参数值
        var param = $('#paramInput').val();

        // 向服务器发送消息
        socket.send(param);

    });

    // 连接关闭时的回调函数
    socket.onclose = function(event) {
        console.log("WebSocket已关闭");
    };

    // 错误处理
    socket.onerror = function(error) {
        console.error("WebSocket错误: ", error);
        $('#responseBox').val('WebSocket连接失败，请检查网络或后端接口状态');
    };
});
</script>

</body>
</html>
